package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcOrderItem;
import com.ruoyi.fun.service.IEcOrderItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单商品Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/fun/item")
public class EcOrderItemController extends BaseController
{
    @Autowired
    private IEcOrderItemService ecOrderItemService;

    /**
     * 查询订单商品列表
     */
    @PreAuthorize("@ss.hasPermi('fun:item:list')")
    @DataScope(userAlias = "o", permission = "fun:item:list")
    @GetMapping("/list")
    public TableDataInfo list(EcOrderItem ecOrderItem)
    {
        startPage();
        Long currentOrgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (currentOrgId != null && currentOrgId == 103L) {
            currentOrgId = null;
        }
        System.out.println("=== EcOrderItemController.list 调试信息 ===");
        System.out.println("当前用户ID: " + getUserId());
        System.out.println("当前部门ID: " + getDeptId());
        System.out.println("当前组织ID: " + currentOrgId);
        System.out.println("传入的查询参数orgId: " + ecOrderItem.getOrgId());
        
        // 添加组织过滤，只显示当前组织的订单商品
        ecOrderItem.setOrgId(currentOrgId);
        System.out.println("设置后的查询参数orgId: " + ecOrderItem.getOrgId());
        
        List<EcOrderItem> list = ecOrderItemService.selectEcOrderItemList(ecOrderItem);
        System.out.println("查询结果数量: " + list.size());
        
        // 调试日志：打印查询结果
        System.out.println("=== 订单商品列表查询结果 ===");
        System.out.println("查询到的记录数: " + (list != null ? list.size() : 0));
        if (list != null && !list.isEmpty()) {
            EcOrderItem firstItem = list.get(0);
            System.out.println("第一条记录的productImage: " + firstItem.getProductImage());
            System.out.println("第一条记录完整信息: " + firstItem.toString());
        }
        
        return getDataTable(list);
    }

    /**
     * 导出订单商品列表
     */
    @PreAuthorize("@ss.hasPermi('fun:item:export')")
    @DataScope(userAlias = "o", permission = "fun:item:export")
    @Log(title = "订单商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcOrderItem ecOrderItem)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecOrderItem.setOrgId(orgId);
        List<EcOrderItem> list = ecOrderItemService.selectEcOrderItemList(ecOrderItem);
        ExcelUtil<EcOrderItem> util = new ExcelUtil<EcOrderItem>(EcOrderItem.class);
        util.exportExcel(response, list, "订单商品数据");
    }

    /**
     * 获取订单商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:item:query')")
    @GetMapping(value = "/{orderItemId}")
    public AjaxResult getInfo(@PathVariable("orderItemId") Long orderItemId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecOrderItemService.selectEcOrderItemByOrderItemIdAndOrgId(orderItemId, orgId));
    }

    /**
     * 新增订单商品
     */
    @PreAuthorize("@ss.hasPermi('fun:item:add')")
    @Log(title = "订单商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcOrderItem ecOrderItem)
    {
        ecOrderItem.setOrgId(getOrgId());
        return toAjax(ecOrderItemService.insertEcOrderItem(ecOrderItem));
    }

    /**
     * 修改订单商品
     */
    @PreAuthorize("@ss.hasPermi('fun:item:edit')")
    @Log(title = "订单商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcOrderItem ecOrderItem)
    {
        return toAjax(ecOrderItemService.updateEcOrderItem(ecOrderItem));
    }

    /**
     * 删除订单商品
     */
    @PreAuthorize("@ss.hasPermi('fun:item:remove')")
    @Log(title = "订单商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderItemIds}")
    public AjaxResult remove(@PathVariable Long[] orderItemIds)
    {
        return toAjax(ecOrderItemService.deleteEcOrderItemByOrderItemIds(orderItemIds));
    }
    /**
     * 根据orderItemId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:ecorderitem:query')")
    @GetMapping(value = "/{orderItemId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("orderItemId") Long orderItemId, @PathVariable("orgId") Long orgId)
    {
        return success(ecOrderItemService.selectEcOrderItemByOrderItemIdAndOrgId(orderItemId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:ecorderitem:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcOrderItem ecOrderitem = new EcOrderItem();
        List<EcOrderItem> list = ecOrderItemService.selectEcOrderItemListByOrgId(ecOrderitem, orgId);
        return getDataTable(list);
    }


}